package org.spring.springboot.service.impl;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/request")
public class DataType {
	 @Autowired
	 private RedisTemplate redisTemplate;
	 
	 @RequestMapping(value = "/test1", method = RequestMethod.GET)
	 public void testString() {  
	        ValueOperations<String, String> ops = redisTemplate.opsForValue();  
	        String key = "helloh";  
	        if (!redisTemplate.hasKey(key)) {  
	            ops.set(key, "fodo"); 
	            System.out.println("Found key " + key + ", value=" + ops.get(key));
	        } else { 
	        	System.out.println("key is " + key + "exists!");
	        	redisTemplate.delete(key);
	        }  
	    }  
	
	 //Redis的哈希键值对的集合。 Redis的哈希值是字符串字段和字符串值之间的映射，所以它们被用来表示对象
	 @RequestMapping(value = "/test2", method = RequestMethod.GET)
	 public void testHashset() {  
	        HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();  
	        String key = "user:1";  
	        if (!redisTemplate.hasKey(key)) {  
	            ops.put(key, "name", "fengchao");  
	            ops.put(key, "sex", "boy");  
	              
	            System.out.println("set succeed");  
	        } else {  
	            System.out.println("key is exist");  
	            Set<Object> keys = ops.keys(key);  
	            for (Object k : keys) {  
	                System.out.println("key " + key );  
	                //删除hash中某一个值
	                if(k.toString().equals("name")){
	                	ops.delete(key, "name");
	                	
	                }
	                
	            }  
	            //删除整个hash的key
	            redisTemplate.delete(key);
	        }  
	    }  

	 /** 
	     * 指定缓存的失效时间 
	     *  
	     * @author FangJun 
	     * @date 2016年8月14日 
	     * @param key 缓存KEY 
	     * @param time 失效时间(秒) 
	     */  
	  /*  public  void expire(String key, ExpireTime time) {  
	        if(time.getTime() > 0){  
	            redisTemplate.expire(key, time.getTime(), TimeUnit.SECONDS);  
	        }  
	    } */ 
	    
	 //Redis的列表是简单的字符串列表，排序插入顺序。可以添加元素到Redis列表的头部或尾部。
	 @RequestMapping(value = "/test3", method = RequestMethod.GET)
	 public void testList() {  
	        ListOperations<String, String> ops = redisTemplate.opsForList();  
	        String key = "88482";  
	          
	        if (!redisTemplate.hasKey(key)) {  
	        	//一次从左边投头push一个
	            ops.leftPush(key, "feng");  
	            ops.leftPush(key, "feng", "||");
	            //一次push多个
	            ops.leftPushAll(key, "cff","cl","mc");
	            
	            //一次从右边push一个
	            ops.rightPush(key, "yw");
	            ops.rightPush( key, "yw", "||");
	            //一次从右边push多个
	            ops.rightPushAll(key, "sj","jy");
	            System.out.println(ops.range(key, 0, -1));
	            System.err.println(ops.index(key, 0));
	            System.out.println("set succeed");  
	        } else {  
	            System.out.println("key is exist");  
	            Long size = ops.size(key);  
	            List<String> list = ops.range(key, 0, size);
	            //取出对应下表元素
	            System.err.println(ops.index(key, 0));
	            for (String value : list) {  
	                System.out.println(value);
	            }  
	            //从左边头部取出，取出后list集合就没这个值了
	            ops.leftPop(key);
	            System.out.println(ops.range(key, 0, size));
	          //从右边头部取出，取出后list集合就没这个值了
	            ops.rightPop(key);
	            System.out.println(ops.range(key, 0, size));

	            //从右边取出一个，同时从左边push一个
	            ops.rightPopAndLeftPush(key, "yyyyy");
	            System.out.println(ops.range(key, 0, size));

	            //指定位置替换元素
	            ops.set(key, 0, "gzf");
	            System.out.println(ops.range(key, 0, size));

	            //指定位置删除元素（成功返回1，失败返回0）
	            //后面跟上一个count value,该指令会删除count个为value的元素; 
	            //如果count大于0,从头向尾遍历并删除count个为value的元素;如果count小于0,从后面向前面去遍历;
	            //如果count等于0,删除链表中所有等于某个值的元素;
	            Long l = ops.remove(key, 3,"feng");
	            System.out.println(l);
	            System.out.println(ops.range(key, 0, size));
	            
	            
	        }  
	    }
	 
	 //Redis集合是字符串的无序集合。在Redis中可以添加，删除和测试文件是否存在在O(1)的时间复杂度的成员。
	 @RequestMapping(value = "/test4", method = RequestMethod.GET)
	 public void testSet() {  
	        SetOperations<String, String> ops = redisTemplate.opsForSet();  
	        String key = "likes";  
	          
	          
	        if (!redisTemplate.hasKey(key)) {  
	            Long add = ops.add(key, "sport","eat","play","play");  
	              
	              
	            System.out.println("set succeed add is" + add);  
	        } else {  
	            Set<String> members = ops.members(key);  
	            for (String value : members) {  
	                System.out.println(value);  
	                ops.remove(key, "sport","eat");
	            }  
	              
	        }  
	    }  
	 
	 //集合排序
	 //Redis的集合排序类似于Redis集合，字符串不重复的集合。不同的是，一个有序集合的每个成员关联分数，
	 //用于以便采取有序set命令，从最小的到最大的分数有关。虽然成员都是独一无二的，分数可能会重复。
	 @RequestMapping(value = "/test5", method = RequestMethod.GET)
	 public void testZList() {  
	        ZSetOperations<String, String> ops = redisTemplate.opsForZSet();  
	        String key = "foode";  
	        if (!redisTemplate.hasKey(key)) { 
	        	//后面的数字是插入顺序
	            ops.add(key, "friute", 0);  
	            ops.add(key, "rice", 2);  
	            ops.add(key, "apple", 1);  
	            ops.add(key, "balane", 3);  
	              
	            System.out.println("set succeed" );  
	        } else {  
	            Set<String> sets = ops.rangeByScore(key, 0, 1000);  
	            for (String value : sets) {  
	                System.out.println(value);  
	            }  
	        }  
	    }  
}
